package l1j.server.GameServer;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.echo.ClientThread;
import l1j.server.server.utils.StreamUtil;
import l1j.server.server.utils.SystemUtil;

public class ServerExecutor extends Thread {

	private ServerSocket _server;

	private int _port = 0;

	private static final Logger _log = Logger.getLogger(ServerExecutor.class.getName());


	public ServerExecutor(final int port) {
		try {
			this._port = port;
			if (!"*".equals(Config.GAME_SERVER_HOST_NAME)) {
				final InetAddress inetaddress = InetAddress.getByName(Config.GAME_SERVER_HOST_NAME);
				//				inetaddress.getHostAddress();
				this._server = new ServerSocket(this._port, 50, inetaddress);
			} else {
				this._server = new ServerSocket(this._port);
			}
		} catch (final SocketTimeoutException e) {
			_log.log(Level.SEVERE ,"連線超時:(" + this._port + ")", e);

		} catch (final IOException e) {
			_log.log(Level.SEVERE ,"IP位置加載錯誤 或 端口位置已被佔用:(" + this._port + ")", e);

		} finally {
			_log.info("[D] " + this.getClass().getSimpleName() + " 開始監聽服務端口:(" + this._port + ")");
		}
	}


	@Override
	public void run() {
		System.out.println("使用記憶體： " + SystemUtil.getUsedMemoryMB() + "MB\n等待玩家連線中...");
		try {
			while (this._server !=null) {
				Socket socket = null;
				
				try {
					socket = this._server.accept();
					if (socket != null) {
						//	Thread.sleep(10);
						//				Socket socket = _serverSocket.accept();
						//	System.out.println("嘗試連線的IP " + socket.getInetAddress());
//						final String host = socket.getInetAddress().getHostAddress();
//						try{
//						bfr = new BufferedReader (new InputStreamReader(socket.getInputStream()));
//						System.out.println("嘗試連線的IP " + bfr.readLine());
//						}catch(Exception e){
//							System.out.println("嘗試連線的IP " + socket.getInetAddress());
//						}
//						LanSecurityManager.BANIPPACK.put(host, 300);
//						socket.setSoTimeout(15000);
						/*	if (IpTable.getInstance().isBannedIp(host)) {
						//	_log.info("禁止 IP(" + host + ")");
						System.out.println("禁止 IP(" + host + ")");
						StreamUtil.close(socket);
						return;
					}*/
						final ClientThread client = new ClientThread(socket);
						GeneralThreadPool.getInstance().execute(client);
						//	IpAttackCheck.SOCKETLIST.put(client, host);
					}
				} catch (final SecurityException e) {
					System.out.println("SecurityException 異常");
				}
			}
		} catch (final IOException e) {
//			_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
		} finally {
			final String lanInfo = "[D] " + this.getClass().getSimpleName() + " 服務器核心關閉監聽端口(" + this._port + ")";
			_log.info(lanInfo);
		}
	}
	/**
	 * 停止監聽
	 */
	public void stopEcho() {
		try {
			if (this._server != null) {
				StreamUtil.close(this._server);
				StreamUtil.interrupt(this);
				this._server = null;
			}

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);

		} finally {

		}
	}


	/**
	 * 啟動監聽
	 * @throws IOException
	 */
	public void stsrtEcho() throws IOException {
		GeneralThreadPool.getInstance().execute(this);
	}



}
